package com.seekting.mynews.connect.tiangou.bean.response;

import android.content.Context;
import android.os.Bundle;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.seekting.mynews.connect.Connector;
import com.seekting.mynews.connect.tiangou.bean.request.TianGouClassifyRequest;
import com.seekting.mynews.connect.tiangou.bean.request.TianGouListRequest;
import com.seekting.mynews.connect.tiangou.bean.request.TianGouShowRequest;
import com.seekting.mynews.connect.tiangou.bean.request.UIDRequest;
import com.seekting.mynews.eventbus.TianGouClassifyEvent;
import com.seekting.mynews.eventbus.TianGouListEvent;
import com.seekting.mynews.eventbus.TianGouShowEvent;
import com.seekting.mynews.manager.Action;
import com.seekting.util.SLog;

import de.greenrobot.event.EventBus;

public class TianGouConnect extends Connector {

    private static final String TAG = "TianGouConnect";
    int classfyCacheDay;
    Response.Listener<TianGouClassify> classifyListener;
    Response.Listener<TianGouList> listListener;
    Response.Listener<TianGouShow> showListener;

    public TianGouConnect(RequestQueue requestQueue, Context mContext) {
        super(requestQueue, mContext);

        classifyListener = new Response.Listener<TianGouClassify>() {

            @Override
            public void onResponse(TianGouClassify tianGouClassify) {

                SLog.d(TAG, "onResponse tianGouClassify" + tianGouClassify);
                TianGouClassifyEvent tianGouClassifyEvent = new TianGouClassifyEvent();
                tianGouClassifyEvent.setTianGouClassify(tianGouClassify);
                EventBus.getDefault().post(tianGouClassifyEvent);
            }
        };
        listListener = new Response.Listener<TianGouList>() {

            @Override
            public void onResponse(TianGouList tianGouList) {
                SLog.d(TAG, "onResponse tianGouList" + tianGouList);
                TianGouListEvent tainGouListEvent = new TianGouListEvent();
                tainGouListEvent.setTianGouList(tianGouList);
                EventBus.getDefault().post(tainGouListEvent);

            }
        };
        showListener = new Response.Listener<TianGouShow>() {

            @Override
            public void onResponse(TianGouShow tianGouShow) {
                SLog.d(TAG, "onResponse tianGouShow" + tianGouShow);
                TianGouShowEvent tainGouShowEvent = new TianGouShowEvent();
                tainGouShowEvent.setTianGouShow(tianGouShow);
                EventBus.getDefault().post(tainGouShowEvent);
            }
        };

    }

    @Override
    public String reqeust(Action action, Bundle extra) {
        String uuid = null;
        UIDRequest request = null;
        int requestFrom = extra.getInt(UIDRequest.REQUEST_FROM, Request.FROM_DISK_HTTP);
        switch (action) {
        case TianGouClassify:
            request = new TianGouClassifyRequest(classifyListener, null);
            break;
        case TianGouList: {
            int id = extra.getInt("id");
            int rows = extra.getInt("rows");
            int page = extra.getInt("page");
            String url = TianGouListRequest.getUrl(id, rows, page);
            request = new TianGouListRequest(url, listListener, null);

        }
            break;
        case TianGouShow:
            int id = extra.getInt("id");
            String url = TianGouShowRequest.getUrl(id);
            request = new TianGouShowRequest(url, showListener, null);

        default:
            break;
        }
        request.setFromType(requestFrom);
        mRequestQueue.add(request);
        return request.getUUid();

    }
}
